<!DOCTYPE HTML>
<html lang="zh-CN">


<head>
    <meta charset="utf-8">
    <meta name="keywords" content="第2章-pod概述.md, 博客，个人经验分离，kubernetes,k8s,Linux运维，Python">
    <meta name="description" content="一、k8s核心资源Pod介绍
K8s官方文档：https://kubernetes.io/
K8s中文官方文档： https://kubernetes.io/zh/
K8s Github地址：https://github.com/kuber">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
    <meta name="renderer" content="webkit|ie-stand|ie-comp">
    <meta name="mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <meta name="referrer" content="no-referrer-when-downgrade">
    <!-- Global site tag (gtag.js) - Google Analytics -->


    <title>第2章-pod概述.md | 躺平的人生</title>
    <link rel="icon" type="image/png" href="/favicon.png">
    


    <!-- bg-cover style     -->



<link rel="stylesheet" type="text/css" href="/libs/awesome/css/all.min.css">
<link rel="stylesheet" type="text/css" href="/libs/materialize/materialize.min.css">
<link rel="stylesheet" type="text/css" href="/libs/aos/aos.css">
<link rel="stylesheet" type="text/css" href="/libs/animate/animate.min.css">
<link rel="stylesheet" type="text/css" href="/libs/lightGallery/css/lightgallery.min.css">
<link rel="stylesheet" type="text/css" href="/css/matery.css">
<link rel="stylesheet" type="text/css" href="/css/my.css">
<link rel="stylesheet" type="text/css" href="/css/dark.css" media="none" onload="if(media!='all')media='all'">




    <link rel="stylesheet" href="/libs/tocbot/tocbot.css">
    <link rel="stylesheet" href="/css/post.css">




    
        <link rel="stylesheet" type="text/css" href="/css/reward.css">
    



    <script src="/libs/jquery/jquery-3.6.0.min.js"></script>

<meta name="generator" content="Hexo 6.3.0"></head>


<body>
    <header class="navbar-fixed">
    <nav id="headNav" class="bg-color nav-transparent">
        <div id="navContainer" class="nav-wrapper container">
            <div class="brand-logo">
                <a href="/" class="waves-effect waves-light">
                    
                    <img src="/medias/logo.png" class="logo-img" alt="LOGO">
                    
                    <span class="logo-span">躺平的人生</span>
                </a>
            </div>
            

<a href="#" data-target="mobile-nav" class="sidenav-trigger button-collapse"><i class="fas fa-bars"></i></a>
<ul class="right nav-menu">
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/" class="waves-effect waves-light">
      
      <i class="fas fa-home" style="zoom: 0.6;"></i>
      
      <span>首页</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/tags" class="waves-effect waves-light">
      
      <i class="fas fa-tags" style="zoom: 0.6;"></i>
      
      <span>标签</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/categories" class="waves-effect waves-light">
      
      <i class="fas fa-bookmark" style="zoom: 0.6;"></i>
      
      <span>分类</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/archives" class="waves-effect waves-light">
      
      <i class="fas fa-archive" style="zoom: 0.6;"></i>
      
      <span>归档</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/about" class="waves-effect waves-light">
      
      <i class="fas fa-user-circle" style="zoom: 0.6;"></i>
      
      <span>关于</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/friends" class="waves-effect waves-light">
      
      <i class="fas fa-address-book" style="zoom: 0.6;"></i>
      
      <span>友情链接</span>
    </a>
    
  </li>
  
  <li>
    <a href="#searchModal" class="modal-trigger waves-effect waves-light">
      <i id="searchIcon" class="fas fa-search" title="搜索" style="zoom: 0.85;"></i>
    </a>
  </li>
  <li>
    <a href="javascript:;" class="waves-effect waves-light" onclick="switchNightMode()" title="深色/浅色模式" >
      <i id="sum-moon-icon" class="fas fa-sun" style="zoom: 0.85;"></i>
    </a>
  </li>
</ul>


<div id="mobile-nav" class="side-nav sidenav">

    <div class="mobile-head bg-color">
        
        <img src="/medias/logo.png" class="logo-img circle responsive-img">
        
        <div class="logo-name">躺平的人生</div>
        <div class="logo-desc">
            
            本站记录本人各种学习的旅途，笔记，用于巩固自我并启发后来人
            
        </div>
    </div>

    <ul class="menu-list mobile-menu-list">
        
        <li class="m-nav-item">
	  
		<a href="/" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-home"></i>
			
			首页
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/tags" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-tags"></i>
			
			标签
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/categories" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-bookmark"></i>
			
			分类
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/archives" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-archive"></i>
			
			归档
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/about" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-user-circle"></i>
			
			关于
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/friends" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-address-book"></i>
			
			友情链接
		</a>
          
        </li>
        
        
        <li><div class="divider"></div></li>
        <li>
            <a href="https://miaohsukang.gitee.io" class="waves-effect waves-light" target="_blank">
                <i class="fab fa-github-square fa-fw"></i>Fork Me
            </a>
        </li>
        
    </ul>
</div>


        </div>

        
            <style>
    .nav-transparent .github-corner {
        display: none !important;
    }

    .github-corner {
        position: absolute;
        z-index: 10;
        top: 0;
        right: 0;
        border: 0;
        transform: scale(1.1);
    }

    .github-corner svg {
        color: #F062A7;
        fill: #fff;
        height: 64px;
        width: 64px;
    }

    .github-corner:hover .octo-arm {
        animation: a 0.56s ease-in-out;
    }

    .github-corner .octo-arm {
        animation: none;
    }

    @keyframes a {
        0%,
        to {
            transform: rotate(0);
        }
        20%,
        60% {
            transform: rotate(-25deg);
        }
        40%,
        80% {
            transform: rotate(10deg);
        }
    }
</style>

<a href="https://miaohsukang.gitee.io" class="github-corner tooltipped hide-on-med-and-down" target="_blank"
   data-tooltip="Fork Me" data-position="left" data-delay="50">
    <svg viewBox="0 0 250 250" aria-hidden="true">
        <path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path>
        <path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2"
              fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path>
        <path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z"
              fill="currentColor" class="octo-body"></path>
    </svg>
</a>
        
    </nav>

</header>

    
<script src="/libs/cryptojs/crypto-js.min.js"></script>
<script>
    (function() {
        let pwd = '';
        if (pwd && pwd.length > 0) {
            if (pwd !== CryptoJS.SHA256(prompt('请输入访问本文章的密码')).toString(CryptoJS.enc.Hex)) {
                alert('密码错误，将返回主页！');
                location.href = '/';
            }
        }
    })();
</script>




<div class="bg-cover pd-header post-cover" style="background-image: url('https://www.zutuanxue.com:8000/static/media/images/2020/10/10/1602326898782.png')">
    <div class="container" style="right: 0px;left: 0px;">
        <div class="row">
            <div class="col s12 m12 l12">
                <div class="brand">
                    <h1 class="description center-align post-title">第2章-pod概述.md</h1>
                </div>
            </div>
        </div>
    </div>
</div>




<main class="post-container content">

    
    <div class="row">
    <div id="main-content" class="col s12 m12 l9">
        <!-- 文章内容详情 -->
<div id="artDetail">
    <div class="card">
        <div class="card-content article-info">
            <div class="row tag-cate">
                <div class="col s7">
                    
                    <div class="article-tag">
                        
                            <a href="/tags/pod/">
                                <span class="chip bg-color">pod</span>
                            </a>
                        
                    </div>
                    
                </div>
                <div class="col s5 right-align">
                    
                    <div class="post-cate">
                        <i class="fas fa-bookmark fa-fw icon-category"></i>
                        
                            <a href="/categories/K8S%E7%AC%94%E8%AE%B0/" class="post-category">
                                K8S笔记
                            </a>
                        
                    </div>
                    
                </div>
            </div>

            <div class="post-info">
                
                <div class="post-date info-break-policy">
                    <i class="far fa-calendar-minus fa-fw"></i>发布日期:&nbsp;&nbsp;
                    2023-02-12
                </div>
                

                
                <div class="post-date info-break-policy">
                    <i class="far fa-calendar-check fa-fw"></i>更新日期:&nbsp;&nbsp;
                    2023-02-12
                </div>
                

                
                <div class="info-break-policy">
                    <i class="far fa-file-word fa-fw"></i>文章字数:&nbsp;&nbsp;
                    6.5k
                </div>
                

                
                <div class="info-break-policy">
                    <i class="far fa-clock fa-fw"></i>阅读时长:&nbsp;&nbsp;
                    27 分
                </div>
                

                
                    <div id="busuanzi_container_page_pv" class="info-break-policy">
                        <i class="far fa-eye fa-fw"></i>阅读次数:&nbsp;&nbsp;
                        <span id="busuanzi_value_page_pv"></span>
                    </div>
				
            </div>
        </div>
        <hr class="clearfix">

        
        <!-- 是否加载使用自带的 prismjs. -->
        <link rel="stylesheet" href="/libs/prism/prism.min.css">
        

        

        <div class="card-content article-card-content">
            <div id="articleContent">
                <h3 id="一、k8s核心资源Pod介绍"><a href="#一、k8s核心资源Pod介绍" class="headerlink" title="一、k8s核心资源Pod介绍"></a>一、k8s核心资源Pod介绍</h3><blockquote>
<p>K8s官方文档：<a target="_blank" rel="noopener" href="https://kubernetes.io/">https://kubernetes.io/</a></p>
<p>K8s中文官方文档： <a target="_blank" rel="noopener" href="https://kubernetes.io/zh/">https://kubernetes.io/zh/</a></p>
<p>K8s Github地址：<a target="_blank" rel="noopener" href="https://github.com/kubernetes/kubernetes">https://github.com/kubernetes/kubernetes</a></p>
<p>Pod资源对应的官方文档：<a target="_blank" rel="noopener" href="https://kubernetes.io/zh-cn/docs/concepts/workloads/pods/">https://kubernetes.io/zh-cn/docs/concepts/workloads/pods/</a></p>
</blockquote>
<h4 id="1-1-Pod是什么？"><a href="#1-1-Pod是什么？" class="headerlink" title="1.1 Pod是什么？"></a>1.1 Pod是什么？</h4><p> 官方文档：<a target="_blank" rel="noopener" href="https://kubernetes.io/docs/concepts/workloads/pods/">https://kubernetes.io/docs/concepts/workloads/pods/</a></p>
<p> Pod是Kubernetes中的最小调度单元，k8s是通过定义一个Pod的资源，然后在Pod里面运行容器，容器需要指定一个镜像，这样就可以用来运行具体的服务。一个Pod封装一个容器（也可以封装多个容器），Pod里的容器共享存储、网络等。也就是说，应该把整个pod看作虚拟机，然后每个容器相当于运行在虚拟机的进程。</p>
<p><img src="https://gitee.com/miaohsukang/myblog/raw/master/img/image-20221104210432157.png"></p>
<p>Pod是需要调度到k8s集群的工作节点来运行的，具体调度到哪个节点，是根据scheduler调度器实现的。</p>
<img src="https://gitee.com/miaohsukang/myblog/raw/master/img/image-20221104210627248.png" alt="image-20221104210627248" style="zoom: 80%;" />

<p> 白话解释： </p>
<p>可以把pod看成是一个“豌豆荚”，里面有很多“豆子”（容器）。一个豌豆荚里可以有很多豆子（容器）</p>
<p><img src="https://gitee.com/miaohsukang/myblog/raw/master/img/image-20221104210544527.png"></p>
<p>pod相当于一个逻辑主机–比方说我们想要部署一个tomcat应用，如果不用容器，我们可能会部署到物理机、虚拟机或者云主机上，那么出现k8s之后，我们就可以定义一个pod资源，在pod里定义一个把tomcat容器，所以pod充当的是一个逻辑主机的角色。</p>
<p>使用 <code>kubectl get po -o wide</code>可以查看 pod 的列表，其中 READY 列代表该 Pod 总共有 1 个容器，并且该容器已经成功启动，可以对外提供服务了</p>
<p><img src="/./../../../../%E7%AC%94%E8%AE%B0/Typora/K8S/photo/image-20221104210727974.png" alt="image-20221104210727974"></p>
<h4 id="1-2-Pod如何管理多个容器？"><a href="#1-2-Pod如何管理多个容器？" class="headerlink" title="1.2 Pod如何管理多个容器？"></a>1.2 Pod如何管理多个容器？</h4><p><img src="https://gitee.com/miaohsukang/myblog/raw/master/img/image-20221104210905909.png"></p>
<p>Pod中可以同时运行多个容器。同一个Pod中的容器会自动的分配到同一个 node 上。同一个Pod中的容器共享资源、网络环境，它们总是被同时调度，在一个Pod中同时运行多个容器是一种比较高级的用法，只有当你的容器需要紧密配合协作的时候才考虑用这种模式。例如，你有一个容器作为web服务器运行，需要用到共享的volume，有另一个“sidecar”容器来从远端获取资源更新这些文件。 </p>
<p> 一些Pod有init容器和应用容器。 在应用程序容器启动之前，运行初始化容器。</p>
<p><img src="https://gitee.com/miaohsukang/myblog/raw/master/img/image-20221104211138506.png"></p>
<h4 id="1-3-Pod网络"><a href="#1-3-Pod网络" class="headerlink" title="1.3 Pod网络"></a>1.3 Pod网络</h4><p>Pod是有IP地址的，假如pod不是共享物理机ip，由网络插件（calico、flannel、weave）划分的ip，每个pod都被分配唯一的IP地址</p>
<p>Docker容器互联的方式：</p>
<p>​    我们在创建新容器的时候，通过–net container参数，指定其和已经存在的某个容器共享一个Network Namespace。如下图所示，右上方黄色新创建的container，其网卡共享左边容器。因此就不会拥有自己独立的 IP，而是共享左边容器的IP 172.17.0.2,端口范围等网络资源</p>
<p><img src="https://gitee.com/miaohsukang/myblog/raw/master/img/image-20221104211750340.png"></p>
<p><strong>和已经存在的none容器共享网络</strong></p>
<p>[root@master1 ~]#<code>docker run --name container2 --net=container:none -it --privileged=true centos</code></p>
<p><strong>Kubernetes中容器共享的方式</strong></p>
<p>在k8s中，启动Pod时，会先启动⼀个pause 的容器，然后将后续的所有容器都 “link 到这个pause 的容器，以实现⽹络共享。</p>
<p><img src="https://gitee.com/miaohsukang/myblog/raw/master/img/image-20221104220044469.png"></p>
<p>实际上，这个 pause 容器，是一个很特殊的容器，它又叫 infra 容器，是每个 Pod 都会自动创建的容器，它不属于用户自定义的容器。</p>
<p><strong>那么这个 pause 容器有什么用呢？</strong></p>
<h5 id="1-3-1-pause容器作用"><a href="#1-3-1-pause容器作用" class="headerlink" title="1.3.1 pause容器作用"></a>1.3.1 pause容器作用</h5><p>一个 Pod 是由一组容器组成的，这些容器之间共享存储和网络资源，那么网络资源是如何共享的呢？</p>
<p>假设现在有一个 Pod，它包含两个容器（A 和 B），K8S 是通过让他们加入（join）另一个第三方容器的 network namespace 实现的共享，而这个第三方容器就是 pause 容器。</p>
<p><img src="https://gitee.com/miaohsukang/myblog/raw/master/img/image-20221104215547061.png"></p>
<p>这么做的目的，其实很简单，想象一下，如果没有这样的第三方容器，会发生怎样的结果？</p>
<p>没有pause容器，那么 A 和 B 要共享网络，要不就是 A 加入 B 的network namespace，要嘛就是 B 加入 A 的network namespace，而无论是谁加入谁，只要network的owner退出了，该Pod里的所有其他容器网络都会立马异常，这显然是不合理的。</p>
<p>反过来，由于pause里只有是挂起一个容器，里面没有任何复杂的逻辑，只要不主动杀掉 Pod，pause 都会一直存活，这样一来就能保证在Pod运行期间同一 Pod 里的容器网络的稳定。</p>
<p>我们在同一 Pod 里所有容器里看到的网络视图，都是完全一样的，包括网络设备、IP 地址、Mac 地址等等，因为他们其实全是同一份，而这一份都来自于 Pod 第一次创建的这个 Infra container。</p>
<p>由于所有的业务容器都要依赖于 pause 容器，因此在Pod启动时，它总是创建的第一个容器，可以说 Pod 的生命周期就是 pause 容器的生命周期。</p>
<h5 id="1-3-2-init容器与应用容器"><a href="#1-3-2-init容器与应用容器" class="headerlink" title="1.3.2 init容器与应用容器"></a>1.3.2 init容器与应用容器</h5><p>init 容器和 pause 容器有相同点，也有不同点</p>
<ul>
<li>相同点在于：它们都有固定用途，是专用的特殊容器</li>
<li>不同点在于：init容器是用户级的容器，它是由用户来定义的，可以有多个，而 pause 容器是系统级容器，它不是由用户定义的，最多只有一个。</li>
</ul>
<p>init 容器会在应用（业务）容器启动之前运行，用来包含一些应用镜像中不存在的实用工具或安装脚本。</p>
<p>init 容器，从名字上来看，也能看出是的用途就是运行一些初始化任务，来保证应用容器运行环境。</p>
<p>这就决定了：</p>
<ul>
<li>init 容器必须先于 应用容器启动</li>
<li>仅当 init 容器完成后，才能运行应用容器</li>
<li>一个 Pod 允许有多个 init 容器，做不同的初始化任务</li>
</ul>
<p>当一个 Pod 有多个 init 容器时，这些 init 容器是顺序运行的，一个 init 容器完成之后，才会运行一个 init 容器。</p>
<p><img src="https://gitee.com/miaohsukang/myblog/raw/master/img/640.png"></p>
<p>因此如果你在 kubectl get po 时加一个 <code>-w</code> 参数，就能看到 Pod 状态的变化过程</p>
<img src="https://gitee.com/miaohsukang/myblog/raw/master/img/640-1667567679583-3.png" alt="图片" style="zoom:80%;" />

<p>在正常情况下（默认 Pod 的 <code>restartPolicy</code> 为 Never），只要有一个 init 容器运行失败，整个 Pod 便会不停地重启，直到 init 容器全部运行完成为止。</p>
<p>而只要一个 Pod 重启，不管init 容器之前有没有执行过，所有的 init 容器都要重新执行一遍。</p>
<p>init 容器与应用容器，除个别配置之外，基本一致，不用付出太多额外的学习成本。</p>
<p>不一样的地方有哪些呢？</p>
<p><strong>第一点不同</strong></p>
<p>定义位置不同。</p>
<p>应用容器定义在 Pod.Spec.Containers，是必填字段，而 init 是定义在 Pod.Spec.initContainers 中，是可选字段。</p>
<p><strong>第二点不同</strong></p>
<p>部分配置不同。</p>
<p>init容器没有Lifecycle actions, Readiness probes, Liveness probes 和 Startup probes，而这些应用容器都有。</p>
<p>另外，虽然 init 容器与应用容器是两个类别的容器，但由于属于同一个 Pod ，因此容器的名字，是不能重复的。</p>
<h4 id="1-4-Pod存储"><a href="#1-4-Pod存储" class="headerlink" title="1.4 Pod存储"></a>1.4 Pod存储</h4><p>创建Pod的时候可以指定挂载的存储卷。 POD中的所有容器都可以访问共享卷，允许这些容器共享数据。 Pod只要挂载持久化数据卷，Pod重启之后数据还是会存在的。</p>
<img src="https://gitee.com/miaohsukang/myblog/raw/master/img/wps1.jpg" alt="img" style="zoom:80%;" />

<h4 id="1-5-之前学习过容器，为什么还需要Pod？"><a href="#1-5-之前学习过容器，为什么还需要Pod？" class="headerlink" title="1.5 之前学习过容器，为什么还需要Pod？"></a>1.5 之前学习过容器，为什么还需要Pod？</h4><ul>
<li><p>Pod是由一组紧耦合的容器组成的容器组，当然目前最流行的就是Docker、containerd、podman容器，Pod就可以作为1或者多个容器的载体。</p>
</li>
<li><p>Pod中的所用容器会被一致调度、同节点部署，并且在一个“共享环境”中运行。把Pod想成一个车：车里面好多座位，每个座位都坐不同的人，每个座位想成是一个容器，这里的“共享环境”包括以下几点：</p>
</li>
</ul>
<p>​	1）所有容器共享一个IP地址和端口空间，意味着容器之间可以通过localhost高效访问，不能有端口冲突</p>
<p>​	2）允许容器之间共享存储卷，通过文件系统交互信息</p>
<ul>
<li>有些容器需要紧密联系，需要一起工作。Pod提供了比容器更高层次的抽象， Pod中的所有容器使用同一个网络的namespace，即相同的IP地址和Port空间。它们可以直接用localhost通信。同样的，这些容器可以共享存储，当K8s挂载Volume到Pod上，本质上是将volume挂载到Pod中的每一个容器里。</li>
</ul>
<h4 id="1-6-代码自动发版更新"><a href="#1-6-代码自动发版更新" class="headerlink" title="1.6 代码自动发版更新"></a>1.6 代码自动发版更新</h4><p><img src="https://gitee.com/miaohsukang/myblog/raw/master/img/image-20221104220754717.png" alt="image-20221104220754717"></p>
<p>假如生产环境部署了一个go的应用，而且部署了几百个节点，希望这个应用可以定时的同步最新的代码，以便自动升级线上环境。这时，我们不希望改动原来的go应用，可以开发一个Git代码仓库的自动同步服务，然后通过Pod的方式进行编排，并共享代码目录，就可以达到更新java应用代码的效果。</p>
<h4 id="1-7-收集业务日志"><a href="#1-7-收集业务日志" class="headerlink" title="1.7 收集业务日志"></a>1.7 收集业务日志</h4><img src="https://gitee.com/miaohsukang/myblog/raw/master/img/image-20221104220834323.png" alt="image-20221104220834323" style="zoom:80%;" />

<p>​	某服务模块已经实现了一些核心的业务逻辑，并且稳定运行了一段时间，日志记录在了某个目录下，按照不同级别分别为 error.log、access.log、warning.log、info.log，现在希望收集这些日志并发送到统一的日志处理服务器上。</p>
<p>​	这时我们可以修改原来的服务模块，在其中添加日志收集、发送的服务，但这样可能会影响原来服务的配置、部署方式，从而带来不必要的问题和成本，也会增加业务逻辑和基础服务的藕合度。</p>
<p>​	如果使用Pod的方式，通过简单的编排，既可以保持原有服务逻辑、部署方式不变，又可以增加新的日志收集服务。</p>
<p>​	而且如果我们对所有服务的日志生成有一个统一的标准，或者仅对日志收集服务稍加修改，就可以将日志收集服务和其他服务进行Pod编排，提供统一、标准的日志收集方式。</p>
<p>​	这里的“核心业务服务”、“日志收集服务”分别是一个镜像，运行在隔离的容器环境中。</p>
<h4 id="1-8-Pod工作方式"><a href="#1-8-Pod工作方式" class="headerlink" title="1.8 Pod工作方式"></a>1.8 Pod工作方式</h4><p>在K8s中，所有的资源都可以使用一个yaml文件来创建，创建Pod也可以使用yaml配置文件。或者使用kubectl run在命令行创建Pod（不常用）。</p>
<h5 id="1-8-1-自主式pod"><a href="#1-8-1-自主式pod" class="headerlink" title="1.8.1 自主式pod"></a>1.8.1 自主式pod</h5><p>所谓的自主式Pod，就是直接定义一个Pod资源，如下：</p>
<pre class="line-numbers language-yaml" data-language="yaml"><code class="language-yaml"><span class="token punctuation">[</span>root@master1 ~<span class="token punctuation">]</span><span class="token comment"># vim pod-tomcat.yaml</span>
<span class="token key atrule">apiVersion</span><span class="token punctuation">:</span> v1
<span class="token key atrule">kind</span><span class="token punctuation">:</span> Pod
<span class="token key atrule">metadata</span><span class="token punctuation">:</span>
  <span class="token key atrule">name</span><span class="token punctuation">:</span> tomcat<span class="token punctuation">-</span>test
  <span class="token key atrule">namespace</span><span class="token punctuation">:</span> default
  <span class="token key atrule">labels</span><span class="token punctuation">:</span>
    <span class="token key atrule">app</span><span class="token punctuation">:</span> tomcat
<span class="token key atrule">spec</span><span class="token punctuation">:</span>
  <span class="token key atrule">containers</span><span class="token punctuation">:</span>
  <span class="token punctuation">-</span> <span class="token key atrule">name</span><span class="token punctuation">:</span> tomcat<span class="token punctuation">-</span>java
    <span class="token key atrule">image</span><span class="token punctuation">:</span> docker.io/xianchao/tomcat<span class="token punctuation">-</span>8.5<span class="token punctuation">-</span>jre8<span class="token punctuation">:</span>v1
    <span class="token key atrule">ports</span><span class="token punctuation">:</span>
    <span class="token punctuation">-</span> <span class="token key atrule">containerPort</span><span class="token punctuation">:</span> <span class="token number">8080</span>
    <span class="token key atrule">imagePullPolicy</span><span class="token punctuation">:</span> IfNotPresent<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>导入镜像</p>
<p>把xianchao-tomcat.tar.gz上传到node1和node2节点，手动解压</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@node1 ~<span class="token punctuation">]</span><span class="token comment"># ctr -n=k8s.io images import  xianchao-tomcat.tar.gz</span>
<span class="token punctuation">[</span>root@node2 ~<span class="token punctuation">]</span><span class="token comment"># ctr -n=k8s.io images import  xianchao-tomcat.tar.gz</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>

<p>更新资源清单文件</p>
<p>[root@master1 ~]# <code>kubectl apply -f pod-tomcat.yaml</code></p>
<p>查看pod是否创建成功</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@master1 pod<span class="token punctuation">]</span><span class="token comment"># kubectl get pods -owide -l app=tomcat</span>
NAME          READY   STATUS    RESTARTS   AGE    IP               NODE    NOMINATED NODE   READINESS GATES
tomcat-test   <span class="token number">1</span>/1     Running   <span class="token number">0</span>          169m   <span class="token number">10.224</span>.166.131   node1   <span class="token operator">&lt;</span>none<span class="token operator">></span>           <span class="token operator">&lt;</span>none<span class="token operator">></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>

<p>但是自主式Pod是存在一个问题的，假如我们不小心删除了pod：</p>
<p>[root@master1 ~]# <code>kubectl delete pods tomcat-test</code></p>
<p>查看pod是否还在</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@master1 pod<span class="token punctuation">]</span><span class="token comment"># kubectl get pods -owide -l app=tomcat</span>
No resources found <span class="token keyword">in</span> default namespace.<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>

<p>结果是空，说明pod已经被删除了</p>
<p>通过上面可以看到，如果直接定义一个Pod资源，那Pod被删除，就彻底被删除了，不会再创建一个新的Pod，这在生产环境还是具有非常大风险的，所以今后我们接触的Pod，都是控制器管理的。</p>
<p>通过上面可以看到，如果直接定义一个Pod资源，那Pod被删除，就彻底被删除了，不会再创建一个新的Pod，这在生产环境还是具有非常大风险的，所以今后我们接触的Pod，都是控制器管理的。</p>
<h4 id="1-9-控制器管理的Pod"><a href="#1-9-控制器管理的Pod" class="headerlink" title="1.9 控制器管理的Pod"></a>1.9 控制器管理的Pod</h4><p>常见的管理Pod的控制器：Replicaset、Deployment、Job、CronJob、Daemonset、Statefulset。</p>
<p>控制器管理的Pod可以确保Pod始终维持在指定的副本数运行,如通过Deployment管理Pod</p>
<p>解压镜像：</p>
<p>把xianchao-nginx.tar.gz上传到node1和node2节点</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@node1 ~<span class="token punctuation">]</span><span class="token comment"># ctr -n=k8s.io images import  xianchao-nginx.tar.gz</span>
<span class="token punctuation">[</span>root@node2 ~<span class="token punctuation">]</span><span class="token comment"># ctr -n=k8s.io images import  xianchao-nginx.tar.gz</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>

<p>创建一个资源清单文件</p>
<pre class="line-numbers language-yaml" data-language="yaml"><code class="language-yaml"><span class="token punctuation">[</span>root@master1 ~<span class="token punctuation">]</span><span class="token comment"># vim nginx-deploy.yaml </span>
<span class="token key atrule">apiVersion</span><span class="token punctuation">:</span> apps/v1
<span class="token key atrule">kind</span><span class="token punctuation">:</span> Deployment
<span class="token key atrule">metadata</span><span class="token punctuation">:</span>
  <span class="token key atrule">name</span><span class="token punctuation">:</span> nginx<span class="token punctuation">-</span>test
  <span class="token key atrule">labels</span><span class="token punctuation">:</span>
    <span class="token key atrule">app</span><span class="token punctuation">:</span> nginx<span class="token punctuation">-</span>deploy
<span class="token key atrule">spec</span><span class="token punctuation">:</span>
  <span class="token key atrule">selector</span><span class="token punctuation">:</span>
    <span class="token key atrule">matchLabels</span><span class="token punctuation">:</span>
      <span class="token key atrule">app</span><span class="token punctuation">:</span> nginx
  <span class="token key atrule">replicas</span><span class="token punctuation">:</span> <span class="token number">2</span>
  <span class="token key atrule">template</span><span class="token punctuation">:</span>
    <span class="token key atrule">metadata</span><span class="token punctuation">:</span>
      <span class="token key atrule">labels</span><span class="token punctuation">:</span>
        <span class="token key atrule">app</span><span class="token punctuation">:</span> nginx
    <span class="token key atrule">spec</span><span class="token punctuation">:</span>
      <span class="token key atrule">containers</span><span class="token punctuation">:</span>
      <span class="token punctuation">-</span> <span class="token key atrule">name</span><span class="token punctuation">:</span> my<span class="token punctuation">-</span>nginx
        <span class="token key atrule">image</span><span class="token punctuation">:</span> docker.io/xianchao/nginx<span class="token punctuation">:</span>v1
        <span class="token key atrule">imagePullPolicy</span><span class="token punctuation">:</span> IfNotPresent
        <span class="token key atrule">ports</span><span class="token punctuation">:</span>
        <span class="token punctuation">-</span> <span class="token key atrule">containerPort</span><span class="token punctuation">:</span> <span class="token number">80</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>更新资源清单文件</p>
<p>[root@master1 ~]#<code>kubectl apply -f nginx-deploy.yaml</code></p>
<p>查看Deployment</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@master1 ~<span class="token punctuation">]</span><span class="token comment">#kubectl get deploy -l app=nginx-deploy</span>
NAME     READY  UP-TO-DATE  AVAILABLE  AGE
nginx-test    <span class="token number">2</span>/2     <span class="token number">2</span>       <span class="token number">2</span>      16s<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>

<p>查看Replicaset</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@master1 ~<span class="token punctuation">]</span><span class="token comment"># kubectl get rs -l app=nginx</span>
NAME           DESIRED  CURRENT  READY  AGE
nginx-test-75c685fdb7  <span class="token number">2</span>     <span class="token number">2</span>     <span class="token number">2</span>    71s<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>

<p>查看pod</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@master1 ~<span class="token punctuation">]</span><span class="token comment"># kubectl get pods -owide -l app=nginx</span>
NAME                         READY   STATUS    RESTARTS   AGE     IP               NODE    NOMINATED NODE   READINESS GATES
nginx-test-d5cc658c5-9zhkq   <span class="token number">1</span>/1     Running   <span class="token number">0</span>          2m39s   <span class="token number">10.224</span>.166.133   node1   <span class="token operator">&lt;</span>none<span class="token operator">></span>           <span class="token operator">&lt;</span>none<span class="token operator">></span>
nginx-test-d5cc658c5-t2rmv   <span class="token number">1</span>/1     Running   <span class="token number">0</span>          2m39s   <span class="token number">10.224</span>.104.3     node2   <span class="token operator">&lt;</span>none<span class="token operator">></span>           <span class="token operator">&lt;</span>none<span class="token operator">></span> <span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>

<p>删除nginx-test-75c685fdb7-9s95h这个pod</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@master1 ~<span class="token punctuation">]</span><span class="token comment"># kubectl delete pods nginx-test-75c685fdb7-9s95h</span>
<span class="token punctuation">[</span>root@master1 ~<span class="token punctuation">]</span><span class="token comment"># kubectl get pods -o wide -l app=nginx</span>
NAME              READY  STATUS        IP             
nginx-test-75c685fdb7-6d4lx   <span class="token number">1</span>/1    Running       <span class="token number">10.244</span>.102.69  
nginx-test-75c685fdb7-pr8gh  <span class="token number">1</span>/1    Running       <span class="token number">10.244</span>.102.70  <span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>发现重新创建一个新的pod是nginx-test-75c685fdb7-pr8gh</p>
<p>通过上面可以发现通过deployment管理的pod，可以确保pod始终维持在指定副本数量</p>
<h3 id="二、如何基于pod运行应用"><a href="#二、如何基于pod运行应用" class="headerlink" title="二、如何基于pod运行应用"></a>二、如何基于pod运行应用</h3><h4 id="2-1-创建Pod的流程"><a href="#2-1-创建Pod的流程" class="headerlink" title="2.1 创建Pod的流程"></a>2.1 创建Pod的流程</h4><img src="https://gitee.com/miaohsukang/myblog/raw/master/img/image-20221104225921752.png" alt="image-20221104225921752" style="zoom:80%;" />

<p><code>kubectl apply -f nginx-deploy.yaml</code>-&gt;找到config文件，基于config文件指定的用户访问指定的集群，这样就找到了apiserver</p>
<ol>
<li>通过 kubectl 命令向 apiserver 提交创建pod的请求，apiserver接收到pod创建请求后，会将pod的属性信息(metadata)写入etcd。</li>
</ol>
<ol start="2">
<li><p>apiserver触发watch机制准备创建pod，信息转发给调度器scheduler，调度器使用调度算法选择node，调度器将node信息给apiserver，apiserver将绑定的node信息写入etcd</p>
</li>
<li><p>apiserver又通过watch机制，调用kubelet，指定pod信息，调用容器运行时创建并启动pod内的容器。</p>
</li>
<li><p>创建完成之后反馈给kubelet, kubelet又将pod的状态信息给apiserver,apiserver又将pod的状态信息写入etcd。</p>
</li>
</ol>
<h4 id="2-2-通过资源清单文件创建第一个Pod"><a href="#2-2-通过资源清单文件创建第一个Pod" class="headerlink" title="2.2 通过资源清单文件创建第一个Pod"></a>2.2 通过资源清单文件创建第一个Pod</h4><pre class="line-numbers language-yaml" data-language="yaml"><code class="language-yaml"><span class="token punctuation">[</span>root@master1 pod<span class="token punctuation">]</span><span class="token comment"># cat pod-first.yaml</span>
<span class="token key atrule">apiVersion</span><span class="token punctuation">:</span> v1
<span class="token key atrule">kind</span><span class="token punctuation">:</span> Pod
<span class="token key atrule">metadata</span><span class="token punctuation">:</span>
  <span class="token key atrule">name</span><span class="token punctuation">:</span> tomcat<span class="token punctuation">-</span>test
  <span class="token key atrule">namespace</span><span class="token punctuation">:</span> default
  <span class="token key atrule">labels</span><span class="token punctuation">:</span>
    <span class="token key atrule">app</span><span class="token punctuation">:</span> tomcat
<span class="token key atrule">spec</span><span class="token punctuation">:</span>
  <span class="token key atrule">containers</span><span class="token punctuation">:</span>
  <span class="token punctuation">-</span> <span class="token key atrule">name</span><span class="token punctuation">:</span> tomcat<span class="token punctuation">-</span>java
    <span class="token key atrule">ports</span><span class="token punctuation">:</span>
    <span class="token punctuation">-</span> <span class="token key atrule">containerPort</span><span class="token punctuation">:</span> <span class="token number">8080</span>
    <span class="token key atrule">image</span><span class="token punctuation">:</span> xianchao/tomcat<span class="token punctuation">-</span>8.5<span class="token punctuation">-</span>jre8<span class="token punctuation">:</span>v1
    <span class="token key atrule">imagePullPolicy</span><span class="token punctuation">:</span> IfNotPresent<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>更新资源清单文件,将pod-first文件中的Pod名改为tomcat-test-1<br>[root@xianchaomaster1 ~]# <code>kubectl apply -f pod-first.yaml</code></p>
<p>查看pod是否创建成功</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@master1 pod<span class="token punctuation">]</span><span class="token comment"># kubectl get pods -l app=tomcat</span>
NAME            READY   STATUS    RESTARTS   AGE
tomcat-test     <span class="token number">1</span>/1     Running   <span class="token number">0</span>          9h
tomcat-test-1   <span class="token number">1</span>/1     Running   <span class="token number">0</span>          3s<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>

<p>查看pod的ip和pod调度到哪个节点上</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@master1 pod<span class="token punctuation">]</span><span class="token comment"># kubectl get pods -owide</span>
NAME                         READY   STATUS    RESTARTS      AGE   IP               NODE    NOMINATED NODE   READINESS GATES
nginx-test-d5cc658c5-9zhkq   <span class="token number">1</span>/1     Running   <span class="token number">0</span>             9h    <span class="token number">10.224</span>.166.133   node1   <span class="token operator">&lt;</span>none<span class="token operator">></span>           <span class="token operator">&lt;</span>none<span class="token operator">></span>
nginx-test-d5cc658c5-9zrtt   <span class="token number">1</span>/1     Running   <span class="token number">0</span>             9h    <span class="token number">10.224</span>.104.4     node2   <span class="token operator">&lt;</span>none<span class="token operator">></span>           <span class="token operator">&lt;</span>none<span class="token operator">></span>
testbusybox                  <span class="token number">1</span>/1     Running   <span class="token number">6</span> <span class="token punctuation">(</span>13m ago<span class="token punctuation">)</span>   11h   <span class="token number">10.224</span>.104.2     node2   <span class="token operator">&lt;</span>none<span class="token operator">></span>           <span class="token operator">&lt;</span>none<span class="token operator">></span>
tomcat-test                  <span class="token number">1</span>/1     Running   <span class="token number">0</span>             9h    <span class="token number">10.224</span>.166.134   node1   <span class="token operator">&lt;</span>none<span class="token operator">></span>           <span class="token operator">&lt;</span>none<span class="token operator">></span>
tomcat-test-1                <span class="token number">1</span>/1     Running   <span class="token number">0</span>             13s   <span class="token number">10.224</span>.166.135   node1   <span class="token operator">&lt;</span>none<span class="token operator">></span>           <span class="token operator">&lt;</span>none<span class="token operator">></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>查看pod日志<br><code>kubectl logs tomcat-test</code></p>
<p>进入到刚才创建的pod，刚才创建的pod名字是web<br><code>kubectl exec -it tomcat-test -- /bin/bash</code></p>
<p>假如pod里有多个容器，进入到pod里的指定容器，按如下命令：<br><code>kubectl exec -it tomcat-test -c tomcat-java -- /bin/bash</code></p>
<p>查看pod详细信息<br><code>kubectl describe pods tomcat-test</code></p>
<p><img src="/./../../../../%E7%AC%94%E8%AE%B0/Typora/K8S/photo/image-20221105085843184.png" alt="image-20221105085843184"></p>
<p>查看pod具有哪些标签：</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@master1 pod<span class="token punctuation">]</span><span class="token comment"># kubectl get pods --show-labels</span>
NAME                         READY   STATUS    RESTARTS      AGE     LABELS
nginx-test-d5cc658c5-9zhkq   <span class="token number">1</span>/1     Running   <span class="token number">0</span>             10h     <span class="token assign-left variable">app</span><span class="token operator">=</span>nginx,pod-template-hash<span class="token operator">=</span>d5cc658c5
nginx-test-d5cc658c5-9zrtt   <span class="token number">1</span>/1     Running   <span class="token number">0</span>             10h     <span class="token assign-left variable">app</span><span class="token operator">=</span>nginx,pod-template-hash<span class="token operator">=</span>d5cc658c5
testbusybox                  <span class="token number">1</span>/1     Running   <span class="token number">7</span> <span class="token punctuation">(</span>75s ago<span class="token punctuation">)</span>   11h     <span class="token assign-left variable">app</span><span class="token operator">=</span>busybox
tomcat-test                  <span class="token number">1</span>/1     Running   <span class="token number">0</span>             9h      <span class="token assign-left variable">app</span><span class="token operator">=</span>tomcat
tomcat-test-1                <span class="token number">1</span>/1     Running   <span class="token number">0</span>             4m32s   <span class="token assign-left variable">app</span><span class="token operator">=</span>tomcat<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>删除pod<br><code>kubectl delete pods tomcat-test</code></p>
<p>或者<br><code>kubectl delete -f pod-first.yaml</code></p>
<p>我们上面创建的pod是一个自主式pod，也就是通过pod创建一个应用程序，如果pod出现故障停掉，那么我们通过pod部署的应用也就会停掉，不安全， 还有一种控制器管理的pod，通过控制器创建pod，可以对pod的生命周期做管理，可以定义pod的副本数，如果有一个pod意外停掉，那么会自动起来一个pod替代之前的pod，之后会讲解pod的控制器</p>
<h4 id="2-3-Pod资源清单字段解读"><a href="#2-3-Pod资源清单字段解读" class="headerlink" title="2.3 Pod资源清单字段解读"></a>2.3 Pod资源清单字段解读</h4><p>Yaml文件内容如下：</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@xianchaomaster1 ~<span class="token punctuation">]</span><span class="token comment"># vim pod-tomcat.yaml</span>
apiVersion: v1  <span class="token comment">#api版本</span>
kind: Pod       <span class="token comment">#创建的资源</span>
metadata:    
  name: tomcat-test  <span class="token comment">#Pod的名字</span>
  namespace: default   <span class="token comment">#Pod所在的名称空间</span>
  labels:
    app:  tomcat     <span class="token comment">#Pod具有的标签</span>
spec:
  containers:
  - name:  tomcat-java   <span class="token comment">#Pod里容器的名字</span>
    ports:
    - containerPort: <span class="token number">8080</span>  <span class="token comment">#容器暴露的端口</span>
    image: xianchao/tomcat-8.5-jre8:v1  <span class="token comment">#容器使用的镜像</span>
  imagePullPolicy: IfNotPresent    <span class="token comment">#镜像拉取策略</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>更新资源清单文件</p>
<p>[root@xianchaomaster1 ~]# <code>kubectl apply -f pod-tomcat.yaml </code></p>
<p>Pod资源清单编写技巧</p>
<p>[root@xianchaomaster1 ~]# <code>kubectl explain pod</code></p>
<p>可以看到 Pod 的一级字段，主要分成 5 个部分：</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">KIND:   Pod
VERSION:  v1
DESCRIPTION:
   Pod is a collection of containers that can run on a host. This resource is created by clients and scheduled onto hosts.
<span class="token comment"># [Pod是可以在主机上运行的容器的集合。此资源是由客户端创建并安排到主机上。]</span>
FIELDS:
  apiVersion	<span class="token operator">&lt;</span>string<span class="token operator">></span>
  APIVersion defines the versioned schema of this representation of an    object. Servers should convert recognized schemas to the latest internal value, and may reject unrecognized values. More info:
   https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md<span class="token comment">#resources</span>
<span class="token comment"># [APIVersion定义了对象,代表了一个版本。]</span>
  kind	<span class="token operator">&lt;</span>string<span class="token operator">></span>
   Kind is a string value representing the REST resource this object represents. Servers may infer this from the endpoint the client submits requests to. Cannot be updated. In CamelCase. More info:
   https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md<span class="token comment">#types-kinds</span>
<span class="token comment"># [Kind是字符串类型的值，代表了要创建的资源。服务器可以从客户端提交的请求推断出这个资源。]</span>
  metadata	<span class="token operator">&lt;</span>Object<span class="token operator">></span>
   Standard object's metadata. More info:
   https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md<span class="token comment">#metadata</span>
<span class="token comment"># [metadata是对象，定义元数据属性信息的]</span>
  spec	<span class="token operator">&lt;</span>Object<span class="token operator">></span>
   Specification of the desired behavior of the pod. More info:
   https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md<span class="token comment">#spec-and-status</span>
<span class="token comment"># [spec制定了定义Pod的规格，里面包含容器的信息]</span>
  status	<span class="token operator">&lt;</span>Object<span class="token operator">></span>
   Most recently observed status of the pod. This data may not be up to date. Populated by the system. Read-only. More info:
   <span class="token punctuation">[</span>https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md<span class="token comment">#spec-and-status](#spec-and-status)</span>
<span class="token comment"># [status表示状态，这个不可以修改，定义pod的时候也不需要定义这个字段]</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>查看pod.metadata字段如何定义</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@xianchaomaster1 ~<span class="token punctuation">]</span><span class="token comment"># kubectl explain pod.metadata</span>
KIND:   Pod
VERSION:  v1
RESOURCE: metadata <span class="token operator">&lt;</span>Object<span class="token operator">></span>
<span class="token comment"># metadata是对象&lt;Object>，下面可以有多个字段</span>
----------------------------------------------
<span class="token punctuation">[</span>root@master1 pod<span class="token punctuation">]</span><span class="token comment"># kubectl explain pod.metadata|grep '&lt;'</span>
RESOURCE: metadata <span class="token operator">&lt;</span>Object<span class="token operator">></span>
   annotations  <span class="token operator">&lt;</span>map<span class="token punctuation">[</span>string<span class="token punctuation">]</span>string<span class="token operator">></span> <span class="token comment"># annotations是注解，map类型表示对应的值是key-value键值对，&lt;string,string>表示 key和value都是String类型的,格式如下：</span>
<span class="token comment">#################################</span>
   <span class="token string">"metadata"</span><span class="token builtin class-name">:</span> <span class="token punctuation">&#123;</span>
  <span class="token string">"annotations"</span><span class="token builtin class-name">:</span> <span class="token punctuation">&#123;</span>
    <span class="token string">"key1"</span> <span class="token builtin class-name">:</span> <span class="token string">"value1"</span>,
    <span class="token string">"key2"</span> <span class="token builtin class-name">:</span> <span class="token string">"value2"</span>
  <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
<span class="token comment">#################################</span>
<span class="token comment">#用Annotation来记录的信息包括：build信息、release信息、Docker镜像信息等，例如时间戳、release id号、镜像hash值、docker registry地址等；日志库、监控库、分析库等资源库的地址信息；程序调试工具信息，例如工具名称、版本号等；团队的联系信息，例如电话号码、负责人名称、网址等</span>
   labels       <span class="token operator">&lt;</span>map<span class="token punctuation">[</span>string<span class="token punctuation">]</span>string<span class="token operator">></span>  <span class="token comment">#创建的资源具有的标签</span>
   name <span class="token operator">&lt;</span>string<span class="token operator">></span>  <span class="token comment">#创建的资源的名字</span>
   namespace    <span class="token operator">&lt;</span>string<span class="token operator">></span>  <span class="token comment">#创建的资源所属的名称空间，namespaces划分了一个空间，在同一个namesace下的资源名字是唯一的，默认的名称空间是default</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>查看pod.spec字段如何定义</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@xianchaomaster1 ~<span class="token punctuation">]</span><span class="token comment"># kubectl explain pod.spec</span>
KIND:     Pod
VERSION:  v1
RESOURCE: spec <span class="token operator">&lt;</span>Object<span class="token operator">></span>
DESCRIPTION:
     Specification of the desired behavior of the pod. More info:
     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md<span class="token comment">#spec-and-status </span>
     PodSpec is a description of a pod.
<span class="token comment"># Pod的spec字段是用来描述Pod的</span>
---------------------------------------------------------------------
<span class="token punctuation">[</span>root@master1 pod<span class="token punctuation">]</span><span class="token comment"># kubectl explain pod.spec|grep '&lt;'</span>
RESOURCE: spec <span class="token operator">&lt;</span>Object<span class="token operator">></span>
   containers   <span class="token operator">&lt;</span><span class="token punctuation">[</span><span class="token punctuation">]</span>Object<span class="token operator">></span> -required-  <span class="token comment">#container是定义在pod里面的，一个pod至少要有一个容器,后面有required表示是必选项</span>
   <span class="token punctuation">..</span><span class="token punctuation">..</span><span class="token punctuation">..</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>查看pod.spec.containers字段如何定义</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@master1 pod<span class="token punctuation">]</span><span class="token comment"># kubectl explain pod.spec.containers|grep '&lt;'</span>
RESOURCE: containers <span class="token operator">&lt;</span><span class="token punctuation">[</span><span class="token punctuation">]</span>Object<span class="token operator">></span>
   image        <span class="token operator">&lt;</span>string<span class="token operator">></span>   <span class="token comment">#image是用来指定容器需要的镜像的</span>
   imagePullPolicy      <span class="token operator">&lt;</span>string<span class="token operator">></span>  <span class="token comment">#镜像拉取策略，pod是要调度到node节点的，那pod启动需要镜像，可以根据这个字段设置镜像拉取策略，支持如下三种：1）Always：不管本地是否存在镜像，都要重新拉取镜像 2）Never： 从不拉取镜像 3）IfNotPresent：如果本地存在，使用本地的镜像，本地不存在，从官方拉取镜像</span>
   name <span class="token operator">&lt;</span>string<span class="token operator">></span> -required-   <span class="token comment">#name是必须字段，用来指定容器名字的</span>
   ports        <span class="token operator">&lt;</span><span class="token punctuation">[</span><span class="token punctuation">]</span>Object<span class="token operator">></span>  <span class="token comment">#port是端口，属于对象列表</span>
   <span class="token punctuation">..</span><span class="token punctuation">..</span><span class="token punctuation">..</span>  <span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>查看pod.spec.container.ports字段如何定义</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@master1 pod<span class="token punctuation">]</span><span class="token comment"># kubectl explain pod.spec.containers.ports|grep '&lt;'</span>
RESOURCE: ports <span class="token operator">&lt;</span><span class="token punctuation">[</span><span class="token punctuation">]</span>Object<span class="token operator">></span>
   containerPort        <span class="token operator">&lt;</span>integer<span class="token operator">></span> -required-  <span class="token comment">#containerPort是必须字段， pod中的容器需要暴露的端口</span>
     number, <span class="token number">0</span> <span class="token operator">&lt;</span> x <span class="token operator">&lt;</span> <span class="token number">65536</span>.
   hostIP       <span class="token operator">&lt;</span>string<span class="token operator">></span>  <span class="token comment">#将容器中的服务暴露到宿主机的端口上时，可以指定绑定的宿主机 IP</span>
   hostPort     <span class="token operator">&lt;</span>integer<span class="token operator">></span>  <span class="token comment">#容器中的服务在宿主机上映射的端口</span>
  <span class="token punctuation">..</span><span class="token punctuation">..</span><span class="token punctuation">..</span>.<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h4 id="2-4-通过kubectl-run创建一个pod"><a href="#2-4-通过kubectl-run创建一个pod" class="headerlink" title="2.4 通过kubectl run创建一个pod"></a>2.4 通过kubectl run创建一个pod</h4><p><code>kubectl run tomcat --image=xianchao/tomcat-8.5-jre8:v1  --image-pull-policy=&#39;IfNotPresent&#39;  --port=8080</code></p>
<p> 查看kubectl的帮助命令：<code>kubectl --help</code></p>
<h4 id="2-5-输出pod中容器的日志"><a href="#2-5-输出pod中容器的日志" class="headerlink" title="2.5 输出pod中容器的日志"></a>2.5 输出pod中容器的日志</h4><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token comment"># 返回仅包含一个容器的pod nginx的日志快照</span>
$ kubectl logs nginx

<span class="token comment"># 返回pod ruby中已经停止的容器web-1的日志快照</span>
$ kubectl logs <span class="token parameter variable">-p</span> <span class="token parameter variable">-c</span> ruby web-1

<span class="token comment"># 持续输出pod ruby中的容器web-1的日志</span>
$ kubectl logs <span class="token parameter variable">-f</span> <span class="token parameter variable">-c</span> ruby web-1

<span class="token comment"># 仅输出pod nginx中最近的20条日志</span>
$ kubectl logs <span class="token parameter variable">--tail</span><span class="token operator">=</span><span class="token number">20</span> nginx

<span class="token comment"># 输出pod nginx中最近一小时内产生的所有日志</span>
$ kubectl logs <span class="token parameter variable">--since</span><span class="token operator">=</span>1h nginx<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>选项说明：</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">-c, <span class="token parameter variable">--container</span><span class="token operator">=</span><span class="token string">""</span><span class="token builtin class-name">:</span> 容器名。
-f, --follow<span class="token punctuation">[</span><span class="token operator">=</span>false<span class="token punctuation">]</span>: 指定是否持续输出日志。
    --interactive<span class="token punctuation">[</span><span class="token operator">=</span>true<span class="token punctuation">]</span>: 如果为true，当需要时提示用户进行输入。默认为true。
    --limit-bytes<span class="token operator">=</span><span class="token number">0</span>: 输出日志的最大字节数。默认无限制。
-p, --previous<span class="token punctuation">[</span><span class="token operator">=</span>false<span class="token punctuation">]</span>: 如果为true，输出pod中曾经运行过，但目前已终止的容器的日志。
    <span class="token parameter variable">--since</span><span class="token operator">=</span><span class="token number">0</span>: 仅返回相对时间范围，如5s、2m或3h，之内的日志。默认返回所有日志。只能同时使用since和since-time中的一种。
    --since-time<span class="token operator">=</span><span class="token string">""</span><span class="token builtin class-name">:</span> 仅返回指定时间（RFC3339格式）之后的日志。默认返回所有日志。只能同时使用since和since-time中的一种。
    <span class="token parameter variable">--tail</span><span class="token operator">=</span>-1: 要显示的最新的日志条数。默认为-1，显示所有的日志。
    --timestamps<span class="token punctuation">[</span><span class="token operator">=</span>false<span class="token punctuation">]</span>: 在日志中包含时间戳。      <span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>继承自父命令的选项</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">    --alsologtostderr<span class="token punctuation">[</span><span class="token operator">=</span>false<span class="token punctuation">]</span>: 同时输出日志到标准错误控制台和文件。
    --api-version<span class="token operator">=</span><span class="token string">""</span><span class="token builtin class-name">:</span> 和服务端交互使用的API版本。
    --certificate-authority<span class="token operator">=</span><span class="token string">""</span><span class="token builtin class-name">:</span> 用以进行认证授权的.cert文件路径。
    --client-certificate<span class="token operator">=</span><span class="token string">""</span><span class="token builtin class-name">:</span> TLS使用的客户端证书路径。
    --client-key<span class="token operator">=</span><span class="token string">""</span><span class="token builtin class-name">:</span> TLS使用的客户端密钥路径。
    <span class="token parameter variable">--cluster</span><span class="token operator">=</span><span class="token string">""</span><span class="token builtin class-name">:</span> 指定使用的kubeconfig配置文件中的集群名。
    <span class="token parameter variable">--context</span><span class="token operator">=</span><span class="token string">""</span><span class="token builtin class-name">:</span> 指定使用的kubeconfig配置文件中的环境名。
    --insecure-skip-tls-verify<span class="token punctuation">[</span><span class="token operator">=</span>false<span class="token punctuation">]</span>: 如果为true，将不会检查服务器凭证的有效性，这会导致你的HTTPS链接变得不安全。
    <span class="token parameter variable">--kubeconfig</span><span class="token operator">=</span><span class="token string">""</span><span class="token builtin class-name">:</span> 命令行请求使用的配置文件路径。
    --log-backtrace-at<span class="token operator">=</span>:0: 当日志长度超过定义的行数时，忽略堆栈信息。
    --log-dir<span class="token operator">=</span><span class="token string">""</span><span class="token builtin class-name">:</span> 如果不为空，将日志文件写入此目录。
    --log-flush-frequency<span class="token operator">=</span>5s: 刷新日志的最大时间间隔。
    --logtostderr<span class="token punctuation">[</span><span class="token operator">=</span>true<span class="token punctuation">]</span>: 输出日志到标准错误控制台，不输出到文件。
    --match-server-version<span class="token punctuation">[</span><span class="token operator">=</span>false<span class="token punctuation">]</span>: 要求服务端和客户端版本匹配。
    <span class="token parameter variable">--namespace</span><span class="token operator">=</span><span class="token string">""</span><span class="token builtin class-name">:</span> 如果不为空，命令将使用此namespace。
    <span class="token parameter variable">--password</span><span class="token operator">=</span><span class="token string">""</span><span class="token builtin class-name">:</span> API Server进行简单认证使用的密码。
-s, <span class="token parameter variable">--server</span><span class="token operator">=</span><span class="token string">""</span><span class="token builtin class-name">:</span> Kubernetes API Server的地址和端口号。
    <span class="token parameter variable">--stderrthreshold</span><span class="token operator">=</span><span class="token number">2</span>: 高于此级别的日志将被输出到错误控制台。
    <span class="token parameter variable">--token</span><span class="token operator">=</span><span class="token string">""</span><span class="token builtin class-name">:</span> 认证到API Server使用的令牌。
    <span class="token parameter variable">--user</span><span class="token operator">=</span><span class="token string">""</span><span class="token builtin class-name">:</span> 指定使用的kubeconfig配置文件中的用户名。
    <span class="token parameter variable">--username</span><span class="token operator">=</span><span class="token string">""</span><span class="token builtin class-name">:</span> API Server进行简单认证使用的用户名。
    <span class="token parameter variable">--v</span><span class="token operator">=</span><span class="token number">0</span>: 指定输出日志的级别。
    <span class="token parameter variable">--vmodule</span><span class="token operator">=</span>: 指定输出日志的模块，格式如下：pattern<span class="token operator">=</span>N，使用逗号分隔。<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h3 id="三、查看容器和Pod的ID"><a href="#三、查看容器和Pod的ID" class="headerlink" title="三、查看容器和Pod的ID"></a>三、查看容器和Pod的ID</h3><h4 id="3-1-容器ID"><a href="#3-1-容器ID" class="headerlink" title="3.1 容器ID"></a>3.1 容器ID</h4><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">root@node1:~<span class="token comment"># kubectl get pods --all-namespaces -o jsonpath='&#123;range .items[*]&#125;&#123;range .status.containerStatuses[*]&#125;&#123;.containerID&#125;&#123;"\n"&#125;&#123;end&#125;'|awk -F/ '&#123;print substr($NF,1,12)&#125;'|head -2</span>
b8e4b6cde268
ac23be45806a
root@node1:~<span class="token comment"># kubectl get pods --all-namespaces -o jsonpath='&#123;range .items[*]&#125;&#123;"pod: "&#125;&#123;.metadata.name&#125;&#123;"\n"&#125;&#123;range .status.containerStatuses[*]&#125;&#123;"\tname: "&#125;&#123;.containerID&#125;&#123;"\n\timage: "&#125;&#123;.image&#125;&#123;"\n"&#125;&#123;end&#125;'|head -6</span>
pod: hello-apparmor
        name: docker://b8e4b6cde268fd6d3554dd7f5298a87928bfe08727b3a71cfcc75f5a4ae0a03e
        image: busybox:latest
pod: calico-kube-controllers-677cd97c8d-pg85g
        name: docker://ac23be45806add466aff00298b64ba31f5746f0f2a29bd87b773dba191893e4d
        image: calico/kube-controllers:v3.18.0<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h4 id="3-2-PodID"><a href="#3-2-PodID" class="headerlink" title="3.2 PodID"></a>3.2 PodID</h4><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">root@master1:~<span class="token comment"># kubectl get pods -n kube-system -o custom-columns=PodName:.metadata.name,PodUID:.metadata.uid</span>
PodName                                    PodUID
calico-kube-controllers-677cd97c8d-pg85g   b2a84c89-7e46-44d3-9c68-71fafe52aa7b
calico-node-2rfsn                          6d9734df-6ef0-47fe-abd6-addc7281acf7
calico-node-qn4h5                          88b23df4-b6f9-46cc-8ff5-eb578fe06995
coredns-65c54cc984-hdrrj                   dbc0784b-75f0-443d-a859-334f5a7c4776
coredns-65c54cc984-hf4td                   a797ad31-7410-4f14-ac54-a4ce2470bbca
etcd-master1                               d0193044-1ab2-47eb-be81-7cc52e716fcf
kube-apiserver-master1                     772d1a94-63a8-4f92-8ba5-2eac30752d9d
kube-controller-manager-master1            c3afc196-6d53-40a6-86d3-994a291d0780
kube-proxy-q7zsx                           b0dcac81-932b-472c-92d3-653e17e9bd49
kube-proxy-t96gf                           e95ebe3d-9612-482b-930d-45448c3ef0f1
kube-scheduler-master1                     a02b2148-0887-4c04-9111-0679f3a37b06
metrics-server-875fcb674-dlwlh             f4d0f273-2155-4ed8-94b6-ae229817d112
-----------------------------------------------------------------------------------------------
root@master1:~<span class="token comment"># kubectl get pod -n kube-system etcd-master1 -o jsonpath='&#123;.metadata.uid&#125;&#123;"\n"&#125;'</span>
d0193044-1ab2-47eb-be81-7cc52e716fcf<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h4 id="3-3-结合"><a href="#3-3-结合" class="headerlink" title="3.3 结合"></a>3.3 结合</h4><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">root@master1:~<span class="token comment"># kubectl get pods --all-namespaces -o=jsonpath='&#123;range .items[*]&#125;[&#123;.status.containerStatuses[0].containerID&#125;, &#123;.status.podIP&#125;]&#123;"\n"&#125;&#123;end&#125;'   </span>
<span class="token punctuation">[</span>docker://e85da75ac64047cc58b12446129d533640cce76d8e512d3d5201fc70b485673b, <span class="token number">10.244</span>.166.140<span class="token punctuation">]</span>
<span class="token punctuation">[</span>docker://ac23be45806add466aff00298b64ba31f5746f0f2a29bd87b773dba191893e4d, <span class="token number">10.244</span>.166.129<span class="token punctuation">]</span>
<span class="token punctuation">[</span>docker://0ae18f6ea0e8892d3391b526ffef409a09f96d92f74a31849c4d8b59577f7863, <span class="token number">10.10</span>.30.140<span class="token punctuation">]</span>
<span class="token punctuation">[</span>docker://868f4d686fc229351fd63bfd285ea2236b97719bbdb84ec19d40def3b7388f11, <span class="token number">10.10</span>.30.141<span class="token punctuation">]</span>
<span class="token punctuation">[</span>docker://8734aa903584d6a1934115fb144c5b2495c88101eb55413cb8d91fc6b630e3d7, <span class="token number">10.244</span>.137.65<span class="token punctuation">]</span>
<span class="token punctuation">[</span>docker://5dd1e534fcdbf44fcfe327582c5cd968c641b9c04dd7203e55af0e337d66a4cc, <span class="token number">10.244</span>.137.66<span class="token punctuation">]</span>
<span class="token punctuation">[</span>docker://12482701961fc4bb3dac8806fe73412c65055c4ef6959b6439d1aa3211c97fe5, <span class="token number">10.10</span>.30.140<span class="token punctuation">]</span>
<span class="token punctuation">[</span>docker://1c6e378c4942b046589340e3314074b55449875e753726cff9b59ade122cbff8, <span class="token number">10.10</span>.30.140<span class="token punctuation">]</span>
<span class="token punctuation">[</span>docker://65de74120f19f72f958b3851347b4e2e9160d03c0818f46e81ce4c3eb1ca471b, <span class="token number">10.10</span>.30.140<span class="token punctuation">]</span>
<span class="token punctuation">[</span>docker://10fde6ee4a98cb83160e7adf384d6c4381b31f270161ebeff22cd2c96c03e695, <span class="token number">10.10</span>.30.140<span class="token punctuation">]</span>
<span class="token punctuation">[</span>docker://5ec1a0637506197f354f8a656c832f97240b8d086902ef15eed271f887a5e174, <span class="token number">10.10</span>.30.141<span class="token punctuation">]</span>
<span class="token punctuation">[</span>docker://c26bd09a47b454986b3c2b7327efc7e66ebede3d605141ccd6e0dac6d4b5baa7, <span class="token number">10.10</span>.30.140<span class="token punctuation">]</span>
<span class="token punctuation">[</span>docker://353afb1a14ed9d16a5618b2002efbe50b64b6ffd1a19e01c00270991d2f19606, <span class="token number">10.244</span>.166.131<span class="token punctuation">]</span>
<span class="token punctuation">[</span>docker://de7dd09ddbab761c20ae44aa30af79f245ccf2da8b26fdc691e148448fcf1e31, <span class="token number">10.244</span>.166.138<span class="token punctuation">]</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>


                
            </div>
            <hr/>

            

    <div class="reprint" id="reprint-statement">
        
            <div class="reprint__author">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-user">
                        文章作者:
                    </i>
                </span>
                <span class="reprint-info">
                    <a href="/about" rel="external nofollow noreferrer">Miaohsukang</a>
                </span>
            </div>
            <div class="reprint__type">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-link">
                        文章链接:
                    </i>
                </span>
                <span class="reprint-info">
                    <a href="https://miaohsukang.gitee.io/2023/02/12/k8s-bi-ji/di-2-zhang-pod-gai-shu-md/">https://miaohsukang.gitee.io/2023/02/12/k8s-bi-ji/di-2-zhang-pod-gai-shu-md/</a>
                </span>
            </div>
            <div class="reprint__notice">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-copyright">
                        版权声明:
                    </i>
                </span>
                <span class="reprint-info">
                    本博客所有文章除特別声明外，均采用
                    <a href="https://creativecommons.org/licenses/by/4.0/deed.zh" rel="external nofollow noreferrer" target="_blank">CC BY 4.0</a>
                    许可协议。转载请注明来源
                    <a href="/about" target="_blank">Miaohsukang</a>
                    !
                </span>
            </div>
        
    </div>

    <script async defer>
      document.addEventListener("copy", function (e) {
        let toastHTML = '<span>复制成功，请遵循本文的转载规则</span><button class="btn-flat toast-action" onclick="navToReprintStatement()" style="font-size: smaller">查看</a>';
        M.toast({html: toastHTML})
      });

      function navToReprintStatement() {
        $("html, body").animate({scrollTop: $("#reprint-statement").offset().top - 80}, 800);
      }
    </script>



            <div class="tag_share" style="display: block;">
                <div class="post-meta__tag-list" style="display: inline-block;">
                    
                        <div class="article-tag">
                            
                                <a href="/tags/pod/">
                                    <span class="chip bg-color">pod</span>
                                </a>
                            
                        </div>
                    
                </div>
                <div class="post_share" style="zoom: 80%; width: fit-content; display: inline-block; float: right; margin: -0.15rem 0;">
                    <link rel="stylesheet" type="text/css" href="/libs/share/css/share.min.css">
<div id="article-share">

    
    <div class="social-share" data-sites="twitter,facebook,google,qq,qzone,wechat,weibo,douban,linkedin" data-wechat-qrcode-helper="<p>微信扫一扫即可分享！</p>"></div>
    <script src="/libs/share/js/social-share.min.js"></script>
    

    

</div>

                </div>
            </div>
            
                <div id="reward">
    <a href="#rewardModal" class="reward-link modal-trigger btn-floating btn-medium waves-effect waves-light red">赏</a>

    <!-- Modal Structure -->
    <div id="rewardModal" class="modal">
        <div class="modal-content">
            <a class="close modal-close"><i class="fas fa-times"></i></a>
            <h4 class="reward-title">你的赏识是我前进的动力</h4>
            <div class="reward-content">
                <div class="reward-tabs">
                    <ul class="tabs row">
                        <li class="tab col s6 alipay-tab waves-effect waves-light"><a href="#alipay">支付宝</a></li>
                        <li class="tab col s6 wechat-tab waves-effect waves-light"><a href="#wechat">微 信</a></li>
                    </ul>
                    <div id="alipay">
                        <img src="/medias/reward/alipay.jpg" class="reward-img" alt="支付宝打赏二维码">
                    </div>
                    <div id="wechat">
                        <img src="/medias/reward/wechat.png" class="reward-img" alt="微信打赏二维码">
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    $(function () {
        $('.tabs').tabs();
    });
</script>

            
        </div>
    </div>

    

    

    

    

    

    

    

    

    

<article id="prenext-posts" class="prev-next articles">
    <div class="row article-row">
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge left-badge text-color">
                <i class="fas fa-chevron-left"></i>&nbsp;上一篇</div>
            <div class="card">
                <a href="/2023/02/14/k8s-bi-ji/di-4-zhang-biao-qian-qin-he-xing-wu-dian-rong-ren-du/">
                    <div class="card-image">
                        
                        <img src="https://raw.githubusercontent.com/miaohsukang/blog-pic/master/202302142203068.jpg" class="responsive-img" alt="第4章-标签亲和性污点容忍度">
                        
                        <span class="card-title">第4章-标签亲和性污点容忍度</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            
                        
                    </div>
                    <div class="publish-info">
                        <span class="publish-date">
                            <i class="far fa-clock fa-fw icon-date"></i>2023-02-14
                        </span>
                        <span class="publish-author">
                            
                            <i class="fas fa-bookmark fa-fw icon-category"></i>
                            
                            <a href="/categories/K8S%E7%AC%94%E8%AE%B0/" class="post-category">
                                    K8S笔记
                                </a>
                            
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/tags/k8s%E6%A0%87%E7%AD%BE/">
                        <span class="chip bg-color">k8s标签</span>
                    </a>
                    
                    <a href="/tags/k8s%E4%BA%B2%E5%92%8C%E6%80%A7/">
                        <span class="chip bg-color">k8s亲和性</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge right-badge text-color">
                下一篇&nbsp;<i class="fas fa-chevron-right"></i>
            </div>
            <div class="card">
                <a href="/2023/02/12/k8s-bi-ji/di-1-zhang-an-zhuang-k8s1-25-gao-ke-yong-ji-qun-md/">
                    <div class="card-image">
                        
                        <img src="https://www.kubernetes.org.cn/img/2020/03/external-etcd.png" class="responsive-img" alt="第1章-安装K8S1.25高可用集群.md">
                        
                        <span class="card-title">第1章-安装K8S1.25高可用集群.md</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            
                        
                    </div>
                    <div class="publish-info">
                            <span class="publish-date">
                                <i class="far fa-clock fa-fw icon-date"></i>2023-02-12
                            </span>
                        <span class="publish-author">
                            
                            <i class="fas fa-bookmark fa-fw icon-category"></i>
                            
                            <a href="/categories/K8S%E7%AC%94%E8%AE%B0/" class="post-category">
                                    K8S笔记
                                </a>
                            
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/tags/k8s%E9%9B%86%E7%BE%A4%E5%AE%89%E8%A3%85/">
                        <span class="chip bg-color">k8s集群安装</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
    </div>
</article>

</div>



<!-- 代码块功能依赖 -->
<script type="text/javascript" src="/libs/codeBlock/codeBlockFuction.js"></script>


  <!-- 是否加载使用自带的 prismjs. -->
  <script type="text/javascript" src="/libs/prism/prism.min.js"></script>


<!-- 代码语言 -->

<script type="text/javascript" src="/libs/codeBlock/codeLang.js"></script>


<!-- 代码块复制 -->

<script type="text/javascript" src="/libs/codeBlock/codeCopy.js"></script>


<!-- 代码块收缩 -->

<script type="text/javascript" src="/libs/codeBlock/codeShrink.js"></script>



    </div>
    <div id="toc-aside" class="expanded col l3 hide-on-med-and-down">
        <div class="toc-widget card" style="background-color: white;">
            <div class="toc-title"><i class="far fa-list-alt"></i>&nbsp;&nbsp;目录</div>
            <div id="toc-content"></div>
        </div>
    </div>
</div>

<!-- TOC 悬浮按钮. -->


<script src="/libs/tocbot/tocbot.min.js"></script>
<script>
    $(function () {
        tocbot.init({
            tocSelector: '#toc-content',
            contentSelector: '#articleContent',
            headingsOffset: -($(window).height() * 0.4 - 45),
            collapseDepth: Number('0'),
            headingSelector: 'h2, h3, h4'
        });

        // Set scroll toc fixed.
        let tocHeight = parseInt($(window).height() * 0.4 - 64);
        let $tocWidget = $('.toc-widget');
        $(window).scroll(function () {
            let scroll = $(window).scrollTop();
            /* add post toc fixed. */
            if (scroll > tocHeight) {
                $tocWidget.addClass('toc-fixed');
            } else {
                $tocWidget.removeClass('toc-fixed');
            }
        });

        
    });
</script>

    

</main>




    <footer class="page-footer bg-color">
    
        <link rel="stylesheet" href="/libs/aplayer/APlayer.min.css">
<style>
    .aplayer .aplayer-lrc p {
        
        display: none;
        
        font-size: 12px;
        font-weight: 700;
        line-height: 16px !important;
    }

    .aplayer .aplayer-lrc p.aplayer-lrc-current {
        
        display: none;
        
        font-size: 15px;
        color: #42b983;
    }

    
    .aplayer.aplayer-fixed.aplayer-narrow .aplayer-body {
        left: -66px !important;
    }

    .aplayer.aplayer-fixed.aplayer-narrow .aplayer-body:hover {
        left: 0px !important;
    }

    
</style>
<div class="">
    
    <div class="row">
        <meting-js class="col l8 offset-l2 m10 offset-m1 s12"
                   server="netease"
                   type="playlist"
                   id="503838841"
                   fixed='true'
                   autoplay='false'
                   theme='#42b983'
                   loop='all'
                   order='random'
                   preload='auto'
                   volume='0.7'
                   list-folded='true'
        >
        </meting-js>
    </div>
</div>

<script src="/libs/aplayer/APlayer.min.js"></script>
<script src="/libs/aplayer/Meting.min.js"></script>

    

    <div class="container row center-align"
         style="margin-bottom: 0px !important;">
        <div class="col s12 m8 l8 copy-right">
            Copyright&nbsp;&copy;
            
                <span id="year">2023</span>
            
            <a href="/about" target="_blank">Miaohsukang</a>
            |&nbsp;Powered by&nbsp;<a href="https://hexo.io/" target="_blank">Hexo</a>
            |&nbsp;Theme&nbsp;<a href="https://miaohsukang.gitee.io" target="_blank">Matery</a>
            
            <br>
            
            
            
                
            
            
                <span id="busuanzi_container_site_pv">
                &nbsp;|&nbsp;<i class="far fa-eye"></i>&nbsp;总访问量:&nbsp;
                    <span id="busuanzi_value_site_pv" class="white-color"></span>
            </span>
            
            
                <span id="busuanzi_container_site_uv">
                &nbsp;|&nbsp;<i class="fas fa-users"></i>&nbsp;总访问人数:&nbsp;
                    <span id="busuanzi_value_site_uv" class="white-color"></span>
            </span>
            
            <br>

            <!-- 运行天数提醒. -->
            
            <br>
            
        </div>
        <div class="col s12 m4 l4 social-link social-statis">
    <a href="https://miaohsukang.gitee.io" class="tooltipped" target="_blank" data-tooltip="访问我的GitHub" data-position="top" data-delay="50">
        <i class="fab fa-github"></i>
    </a>



    <a href="mailto:12419315+miaohsukang@user.noreply.gitee.com" class="tooltipped" target="_blank" data-tooltip="邮件联系我" data-position="top" data-delay="50">
        <i class="fas fa-envelope-open"></i>
    </a>



    <a href="tencent://AddContact/?fromId=50&fromSubId=1&subcmd=all&uin=258603904" class="tooltipped" target="_blank" data-tooltip="QQ联系我: 258603904" data-position="top" data-delay="50">
        <i class="fab fa-qq"></i>
    </a>

</div>
    </div>
</footer>

<div class="progress-bar"></div>


    <!-- 搜索遮罩框 -->
<div id="searchModal" class="modal">
    <div class="modal-content">
        <div class="search-header">
            <span class="title"><i class="fas fa-search"></i>&nbsp;&nbsp;搜索</span>
            <input type="search" id="searchInput" name="s" placeholder="请输入搜索的关键字"
                   class="search-input">
        </div>
        <div id="searchResult"></div>
    </div>
</div>

<script type="text/javascript">
$(function () {
    var searchFunc = function (path, search_id, content_id) {
        'use strict';
        $.ajax({
            url: path,
            dataType: "xml",
            success: function (xmlResponse) {
                // get the contents from search data
                var datas = $("entry", xmlResponse).map(function () {
                    return {
                        title: $("title", this).text(),
                        content: $("content", this).text(),
                        url: $("url", this).text()
                    };
                }).get();
                var $input = document.getElementById(search_id);
                var $resultContent = document.getElementById(content_id);
                $input.addEventListener('input', function () {
                    var str = '<ul class=\"search-result-list\">';
                    var keywords = this.value.trim().toLowerCase().split(/[\s\-]+/);
                    $resultContent.innerHTML = "";
                    if (this.value.trim().length <= 0) {
                        return;
                    }
                    // perform local searching
                    datas.forEach(function (data) {
                        var isMatch = true;
                        var data_title = data.title.trim().toLowerCase();
                        var data_content = data.content.trim().replace(/<[^>]+>/g, "").toLowerCase();
                        var data_url = data.url;
                        data_url = data_url.indexOf('/') === 0 ? data.url : '/' + data_url;
                        var index_title = -1;
                        var index_content = -1;
                        var first_occur = -1;
                        // only match artiles with not empty titles and contents
                        if (data_title !== '' && data_content !== '') {
                            keywords.forEach(function (keyword, i) {
                                index_title = data_title.indexOf(keyword);
                                index_content = data_content.indexOf(keyword);
                                if (index_title < 0 && index_content < 0) {
                                    isMatch = false;
                                } else {
                                    if (index_content < 0) {
                                        index_content = 0;
                                    }
                                    if (i === 0) {
                                        first_occur = index_content;
                                    }
                                }
                            });
                        }
                        // show search results
                        if (isMatch) {
                            str += "<li><a href='" + data_url + "' class='search-result-title'>" + data_title + "</a>";
                            var content = data.content.trim().replace(/<[^>]+>/g, "");
                            if (first_occur >= 0) {
                                // cut out 100 characters
                                var start = first_occur - 20;
                                var end = first_occur + 80;
                                if (start < 0) {
                                    start = 0;
                                }
                                if (start === 0) {
                                    end = 100;
                                }
                                if (end > content.length) {
                                    end = content.length;
                                }
                                var match_content = content.substr(start, end);
                                // highlight all keywords
                                keywords.forEach(function (keyword) {
                                    var regS = new RegExp(keyword, "gi");
                                    match_content = match_content.replace(regS, "<em class=\"search-keyword\">" + keyword + "</em>");
                                });

                                str += "<p class=\"search-result\">" + match_content + "...</p>"
                            }
                            str += "</li>";
                        }
                    });
                    str += "</ul>";
                    $resultContent.innerHTML = str;
                });
            }
        });
    };

    searchFunc('/search.xml', 'searchInput', 'searchResult');
});
</script>

    <!-- 白天和黑夜主题 -->
<div class="stars-con">
    <div id="stars"></div>
    <div id="stars2"></div>
    <div id="stars3"></div>  
</div>

<script>
    function switchNightMode() {
        $('<div class="Cuteen_DarkSky"><div class="Cuteen_DarkPlanet"></div></div>').appendTo($('body')),
        setTimeout(function () {
            $('body').hasClass('DarkMode') 
            ? ($('body').removeClass('DarkMode'), localStorage.setItem('isDark', '0'), $('#sum-moon-icon').removeClass("fa-sun").addClass('fa-moon')) 
            : ($('body').addClass('DarkMode'), localStorage.setItem('isDark', '1'), $('#sum-moon-icon').addClass("fa-sun").removeClass('fa-moon')),
            
            setTimeout(function () {
            $('.Cuteen_DarkSky').fadeOut(1e3, function () {
                $(this).remove()
            })
            }, 2e3)
        })
    }
</script>

    <!-- 回到顶部按钮 -->
<div id="backTop" class="top-scroll">
    <a class="btn-floating btn-large waves-effect waves-light" href="#!">
        <i class="fas fa-arrow-up"></i>
    </a>
</div>


    <script src="/libs/materialize/materialize.min.js"></script>
    <script src="/libs/masonry/masonry.pkgd.min.js"></script>
    <script src="/libs/aos/aos.js"></script>
    <script src="/libs/scrollprogress/scrollProgress.min.js"></script>
    <script src="/libs/lightGallery/js/lightgallery-all.min.js"></script>
    <script src="/js/matery.js"></script>

    

    
    
    

    <!-- 雪花特效 -->
    

    <!-- 鼠标星星特效 -->
    

     
        <script src="https://ssl.captcha.qq.com/TCaptcha.js"></script>
        <script src="/libs/others/TencentCaptcha.js"></script>
        <button id="TencentCaptcha" data-appid="xxxxxxxxxx" data-cbfn="callback" type="button" hidden></button>
    

    <!-- Baidu Analytics -->

    <!-- Baidu Push -->

<script>
    (function () {
        var bp = document.createElement('script');
        var curProtocol = window.location.protocol.split(':')[0];
        if (curProtocol === 'https') {
            bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
        } else {
            bp.src = 'http://push.zhanzhang.baidu.com/push.js';
        }
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(bp, s);
    })();
</script>

    
    <script src="/libs/others/clicklove.js" async="async"></script>
    
    
    <script async src="/libs/others/busuanzi.pure.mini.js"></script>
    

    

    

    <!--腾讯兔小巢-->
    
    

    

    

    
    <script src="/libs/instantpage/instantpage.js" type="module"></script>
    

</body>

</html>
